using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Castle.Windsor;
using Castle.MicroKernel;

namespace DecisionMaking.WebApp
{
	/// <summary>
	/// Controller Factory class for instantiating controllers using the Windsor IoC container.
	/// </summary>
	public class WindsorControllerFactory : DefaultControllerFactory
	{
		private IKernel kernel;

		/// <summary>
		/// Creates a new instance of the <see cref="WindsorControllerFactory"/> class.
		/// </summary>
		/// <param name="container">The Windsor container instance to use when creating controllers.</param>
		public WindsorControllerFactory (IKernel kernel)
		{
			if (kernel == null) {
				throw new ArgumentNullException ("kernel");
			}
			this.kernel = kernel;
		}

		protected override IController GetControllerInstance (RequestContext context, Type controllerType)
		{
			if (controllerType == null) {
				throw new HttpException (404, string.Format ("The controller for path '{0}' could not be found or it does not implement IController.", context.HttpContext.Request.Path));
			}
			
			return (IController)kernel.Resolve (controllerType);
		}

		public override void ReleaseController (IController controller)
		{
			var disposable = controller as IDisposable;
			
			if (disposable != null) {
				disposable.Dispose ();
			}
			
			kernel.ReleaseComponent(controller);
		}
	}
}
